package de.no3x.learn.rechner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class FormularActivity extends Activity {
	public static final int AUSRECHNEN_ID = Menu.FIRST;

	public static final String BETRAG_KEY = "betrag";
	public static final String BETRAG_ART = "art";
	public static final String UST_PROZENT = "ust";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, AUSRECHNEN_ID, Menu.NONE, "Umrechnen");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// (1)

		switch (item.getItemId()) { // (2)
		case AUSRECHNEN_ID:
			// Betrag:
			final EditText txtBetrag = (EditText) findViewById(R.id.edt_betrag); // (3)
			final float betrag = Float.parseFloat(txtBetrag.getText()
					.toString()); // (4)

			// Art des Betrags (Brutto, Netto):
			boolean isNetto = true;
			final RadioGroup rg = (RadioGroup) findViewById(R.id.rg_art);
			switch (rg.getCheckedRadioButtonId()) {
			case R.id.rb_art_netto:
				isNetto = true;
				break;
			case R.id.rb_art_brutto:
				isNetto = false;
				break;
			default:
			}

			// Prozentwert Umsatzsteuer:
			final Spinner spinner = (Spinner) findViewById(R.id.sp_umsatzsteuer);
			final int pos = spinner.getSelectedItemPosition();
			final int[] prozentwerte = getResources().getIntArray(
					R.array.ust_werte);
			final int prozentwert = prozentwerte[pos];
			final Intent intent = new Intent(this,  // (5) 
					ErgebnisActivity.class);
			intent.putExtra(BETRAG_KEY, betrag); // (6)

			intent.putExtra(BETRAG_ART, isNetto);
			intent.putExtra(UST_PROZENT, prozentwert);
			startActivity(intent); // (7)

		default:
		}
		return super.onOptionsItemSelected(item);
	}
}